#include "blockingqueue.hpp"
#include "Task.hpp"
#include <ctime>
#include <string>
#include <sys/types.h>
#include <unistd.h>

using namespace std;

pthread_mutex_t cout_lock = PTHREAD_MUTEX_INITIALIZER;

const string oper = "+-*/%";

template <class T1, class T2>
class Queues
{
public:
    Queues()
    {
    }

    Queues(BlockQueue<T1> *bq1, BlockQueue<T2> *bq2)
        : _bq1(bq1), _bq2(bq2)
    {
    }

    ~Queues()
    {
    }

public:
    BlockQueue<T1> *_bq1;
    BlockQueue<T2> *_bq2;
};

void *productor(void *args)
{
    BlockQueue<CalTask> *bq1 = (static_cast<Queues<CalTask, SaveTask> *>(args))->_bq1;

    while (true)
    {
        // 生产活动
        int x = rand() % 500 + 1;              // 1 - 500
        int y = rand() % 200;                  // 0 - 200
        int oper_index = rand() % oper.size(); // 0 - 4
        CalTask t(x, y, oper[oper_index], mymath);

        bq1->push(t);

        pthread_mutex_lock(&cout_lock);
        cout << "生产任务 : " << t.task_to_string() << endl;
        pthread_mutex_unlock(&cout_lock);

        // sleep(1);
        usleep(50000);     // 0.5s
    }

    return nullptr;
}

void *consumer(void *args)
{
    BlockQueue<CalTask> *bq1 = (static_cast<Queues<CalTask, SaveTask> *>(args))->_bq1;
    BlockQueue<SaveTask> *bq2 = (static_cast<Queues<CalTask, SaveTask> *>(args))->_bq2;

    while (true)
    {
        // 消费活动
        CalTask t;
        bq1->pop(&t);
        string res = t();

        pthread_mutex_lock(&cout_lock);
        cout << "消费任务 : ";
        cout << res << endl;
        pthread_mutex_unlock(&cout_lock);

        // 推送任务
        SaveTask st(res, save);
        bq2->push(st);
    }

    return nullptr;
}

void *saver(void *args)
{
    BlockQueue<SaveTask> *bq2 = (static_cast<Queues<CalTask, SaveTask> *>(args))->_bq2;

    while (true)
    {
        SaveTask st;
        bq2->pop(&st);
        st();

        pthread_mutex_lock(&cout_lock);
        cout << "保存任务成功... " << endl;
        pthread_mutex_unlock(&cout_lock);
        
    }

    return nullptr;
}

int main()
{
    srand((unsigned int)time(nullptr) ^ getpid() ^ 2024);

    Queues<CalTask, SaveTask> bqs;
    bqs._bq1 = new BlockQueue<CalTask>();
    bqs._bq2 = new BlockQueue<SaveTask>();

    pthread_t s[2], c[3], p[4];

    pthread_create(p, nullptr, productor, &bqs);
    pthread_create(p + 1, nullptr, productor, &bqs);
    pthread_create(p + 2, nullptr, productor, &bqs);
    pthread_create(p + 3, nullptr, productor, &bqs);
    pthread_create(c, nullptr, consumer, &bqs);
    pthread_create(c + 1, nullptr, consumer, &bqs);
    pthread_create(c + 2, nullptr, consumer, &bqs);
    pthread_create(s, nullptr, saver, &bqs);
    pthread_create(s + 1, nullptr, saver, &bqs);

    pthread_join(p[0], nullptr);
    pthread_join(p[1], nullptr);
    pthread_join(p[2], nullptr);
    pthread_join(p[3], nullptr);
    pthread_join(c[0], nullptr);
    pthread_join(c[1], nullptr);
    pthread_join(c[2], nullptr);
    pthread_join(s[0], nullptr);
    pthread_join(s[1], nullptr);

    delete bqs._bq1;
    delete bqs._bq2;
    return 0;
}